package ejb.cbm.seguridad.session;

import java.util.List;

import javax.persistence.EntityManager;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ejb.cbm.commons.JdbcHelper;
import ejb.cbm.seguridad.bean.RegistroPerfilBean;
import ejb.cbm.seguridad.entity.RegistroPerfilEntity;
import ejb.cbm.seguridad.local.RegistroPerfilSessionLocal;

////@Stateless
public class RegistroPerfilSession extends HibernateDaoSupport implements RegistroPerfilSessionLocal {
	
	static Logger logger = Logger.getLogger(RegistroPerfilSession.class.getName());
	//property constants
	public static final String CODIGOPERFIL = "codigoperfil";
	public static final String TIPOREGISTRO = "tiporegistro";
	public static final String VALORREGISTRO = "valorregistro";
	public static final String DESCRIPCION = "descripcion";
	public static final String FLAGACTIVO = "flagactivo";
	public static final String USUARIOREGISTRO = "usuarioregistro";
	public static final String USUARIOMODIFICACION = "usuariomodificacion";


	@Autowired
	public RegistroPerfilSession(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
    //@PersistenceContext 
	//private EntityManager entityManager;
    public void save(RegistroPerfilEntity transientInstance) {
    	logger.log("saving RegistroPerfil instance", Level.INFO, null,null);
        try {
            getSession().persist(transientInstance);
            logger.log("save successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("save failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public void delete(RegistroPerfilEntity persistentInstance) {
    	logger.log("deleting RegistroPerfil instance", Level.INFO, null,null);
        try {
            getSession().delete(persistentInstance);
            logger.log("delete successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("delete failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public RegistroPerfilEntity update(RegistroPerfilEntity detachedInstance) {
    	logger.log("updating RegistroPerfil instance", Level.INFO,null, null);
        try {
            RegistroPerfilEntity result = (RegistroPerfilEntity) getSession().merge(detachedInstance);
            logger.log("update successful", Level.INFO, null,null);
            return result;
        } catch (RuntimeException re) {
        	logger.log("update failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public RegistroPerfilEntity findById( Long id) {
    	logger.log("finding RegistroPerfil instance with id: " + id, Level.INFO,null, null);
        try {
            RegistroPerfilEntity instance = (RegistroPerfilEntity) getSession().get(RegistroPerfilEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
        	logger.log("find failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    //@SuppressWarnings("unchecked")
    public List<RegistroPerfilEntity> findByProperty(String propertyName, Object value) {
    	logger.log("finding RegistroPerfil instance with property: " + propertyName
    						+ ", value: " + value, Level.INFO, null,null);
		try {
			String queryString = "select model from RegistroPerfil model where model." 
			 						+ propertyName + "= :propertyValue";
			return getSession().createQuery(queryString)
 				.setParameter("propertyValue", value).list();
		} catch (RuntimeException re) {
			logger.log("find by property name failed", Level.FATAL,null, re);
			throw re;
		}
	}
	
	public List<RegistroPerfilEntity> findByCodigoperfil(Object codigoperfil) {
		return findByProperty(CODIGOPERFIL, codigoperfil);
	}
	
	public List<RegistroPerfilEntity> findByTiporegistro(Object tiporegistro) {
		return findByProperty(TIPOREGISTRO, tiporegistro);
	}
	
	public List<RegistroPerfilEntity> findByValorregistro(Object valorregistro) {
		return findByProperty(VALORREGISTRO, valorregistro);
	}
	
	public List<RegistroPerfilEntity> findByDescripcion(Object descripcion) {
		return findByProperty(DESCRIPCION, descripcion);
	}
	
	public List<RegistroPerfilEntity> findByFlagactivo(Object flagactivo) {
		return findByProperty(FLAGACTIVO, flagactivo);
	}
	
	public List<RegistroPerfilEntity> findByUsuarioregistro(Object usuarioregistro) {
		return findByProperty(USUARIOREGISTRO, usuarioregistro);
	}
	
	public List<RegistroPerfilEntity> findByUsuariomodificacion(Object usuariomodificacion) {
		return findByProperty(USUARIOMODIFICACION, usuariomodificacion);
	}
	

	//@SuppressWarnings("unchecked")
	public List<RegistroPerfilEntity> findAll() {
		logger.log("finding all RegistroPerfil instances", Level.INFO, null,null);
		try {
			String queryString = "select model from RegistroPerfil model";
			return getSession().createQuery(queryString).list();
		} catch (RuntimeException re) {
			logger.log("find all failed", Level.FATAL,null, re);
			throw re;
		}
	}
	
	//@SuppressWarnings("unchecked")
	public List<RegistroPerfilEntity> findByFiltro(RegistroPerfilBean filtro){
		logger.log("UsuarioSession.findByFiltro UsuarioEntity instance", Level.INFO, null, null);
		StringBuilder query = new StringBuilder();
		
		try{
			query.append(" select model ");
			query.append(" from RegistroPerfilEntity model ");
			query.append(" where 1 = 1 ");
			
			if(filtro.getIdregistroperfil() != null){
				query.append(" and model.idregistroperfil = "+filtro.getIdregistroperfil());
			}
			if(filtro.getCodigoperfil() != null){
				query.append(" and model.codigoperfil = "+filtro.getCodigoperfil());
			}
			if(filtro.getTiporegistro()!= null){
				query.append(" and model.tiporegistro = "+filtro.getTiporegistro());
			}
			if(filtro.getValorregistro() != null){
				query.append(" and model.valorregistro = "+JdbcHelper.to_char(filtro.getValorregistro()));
			}
			if(filtro.getDescripcion()!= null){
				query.append(" and model.descripcion = "+JdbcHelper.to_char(filtro.getDescripcion()));
			}
			if(filtro.getFlagactivo()!= null){
				query.append(" and model.flagactivo = "+filtro.getFlagactivo());
			}
			query.append(" order by model.idregistroperfil ");

			return getSession().createQuery(query.toString()).list();
		
		}catch(RuntimeException e){
			logger.log("UsuarioSession.findByFiltro failed", Level.FATAL, null, e);
			throw e;
		}
	}
}